<?php
namespace Admin\Model;
use Think\Model;
class MemberModel extends Model 
{
	protected $insertFields = array('username','password','cpassword','jifen','chkcode','must_click');
	protected $updateFields = array('id','username','password','cpassword','jifen','must_click');
	protected $_validate = array(
	    /*array('must_click','require','必须同意注册协议!',1,'regex',3),*/
		array('username', 'require', '用户名不能为空！', 1, 'regex', 3),
		array('username', '1,30', '用户名的值最长不能超过 30 个字符！', 1, 'length', 3),
		array('password', 'require', '密码不能为空！', 1, 'regex', 3),
		array('password', '1,30', '密码的值最长不能超过 30 个字符！', 1, 'length', 3),
       /* array('cpassword','password','两次输入的密码不一致!',1,'confirm',3),*/
        /*array('username','','用户名已经存在!',1,'unique',3),*/
        array('chkcode' ,'require','验证码不能为空!',1,'regex',3),
        array('chkcode','check_verify','验证码错误!',1,'callback'),
		array('jifen', 'number', '积分必须是一个整数！', 2, 'regex', 3),
	);

    protected $_login_validate = array(
        array('username','require','用户名不能为空!',1),
        array('password','require','密码不能为空!',1),
        array('chkcode','require','验证码不能为空!',1),
        array('chkcode','check_verify','验证码不正确!',2,'callback'),
    );

    public function check_verify($code,$id=''){
        $verify = new \Think\Verify();
        return $verify->check($code,$id);
    }

    //$needPassword=true
    public function login($needPassword = true) {
        $username = $this->username;
        $password = $this->password;
        $user = $this->field('id,username,password,jifen')->where(array(
            'username'=> array('eq',$username),
        ))->find();
        //var_dump($user);exit;

        if ($user) {

            if ($needPassword) {

                if ($user['password'] == ($password)) {//MD5
                    //存session
                    session('m_id',$user['id']);
                    session('m_username',$user['username']);
                    session('face','/Public/Home/images/user1.gif');
                    //计算会员级别
                    $mlModel = D('member_level');
                    $levelId = $mlModel->field('id')->where(array(
                        'jifen_bottom' => array('elt',$user['jifen']),
                        'jifen_top'  => array('egt',$user['jifen']),
                    ))->find();
                    session('level_id',$levelId['id']);
                    //
                    $cartModel = D('home/Cart');
                    //$cartModel->moveDataToDb();
                    return true;
                } else {
                    $this->error = '密码不正确!';
                    return false;
                }
            }else {
                return $this->login(true);
            }
        } else {
            $this->error = '用户名不存在!';
            return false;
        }
    }

	public function search($pageSize = 20)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		if($username = I('get.username'))
			$where['username'] = array('like', "%$username%");
		if($password = I('get.password'))
			$where['password'] = array('like', "%$password%");
		if($jifen = I('get.jifen'))
			$where['jifen'] = array('eq', $jifen);
		/************************************* 翻页 ****************************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this->alias('a')->where($where)->group('a.id')->limit($page->firstRow.','.$page->listRows)->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
		if(isset($_FILES['face']) && $_FILES['face']['error'] == 0)
		{
			$ret = uploadOne('face', 'Member', array(
				array(350, 350, 2),
				array(150, 150, 2),
				array(50, 50, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['face'] = $ret['images'][0];
				$data['big_face'] = $ret['images'][1];
				$data['mid_face'] = $ret['images'][2];
				$data['sm_face'] = $ret['images'][3];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
		}
	}
	// 修改前
	protected function _before_update(&$data, $option)
	{
		if(isset($_FILES['face']) && $_FILES['face']['error'] == 0)
		{
			$ret = uploadOne('face', 'Member', array(
				array(350, 350, 2),
				array(150, 150, 2),
				array(50, 50, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['face'] = $ret['images'][0];
				$data['big_face'] = $ret['images'][1];
				$data['mid_face'] = $ret['images'][2];
				$data['sm_face'] = $ret['images'][3];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
			deleteImage(array(
				I('post.old_face'),
				I('post.old_big_face'),
				I('post.old_mid_face'),
				I('post.old_sm_face'),
	
			));
		}
	}
	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
		$images = $this->field('face,big_face,mid_face,sm_face')->find($option['where']['id']);
		deleteImage($images);
	}
	/************************************ 其他方法 ********************************************/
}